package com.zelin.rabbit.listener;

import com.alibaba.fastjson.JSON;
import com.aliyuncs.exceptions.ClientException;
import lombok.experimental.var;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import util.SmsUtil;

import java.util.HashMap;
import java.util.Map;

@RabbitListener(queues = "sms")
@Component
public class SmsListener {

    @Autowired
    private SmsUtil smsUtil;

    @RabbitHandler
    public void sendSms(Map map){
        try {
            //1.接收到手机号和验证码
            String mobile = (String) map.get("mobile");
            String code = (String) map.get("code");
            //2.转换数据
            HashMap hashMap = new HashMap<>();
            hashMap.put("code",code);
            String param = JSON.toJSONString(hashMap);
            //3.向阿里大于发送短信验证码
            smsUtil.sendSms(mobile,"SMS_178771364","品优购",param);
        } catch (ClientException e) {
            e.printStackTrace();
        }

    }

}
